/* eslint-disable no-var */
var CryptoJS = require('crypto-js');
// import { CryptoJS } from 'crypto-js';

//密钥
const key = CryptoJS.enc.Utf8.parse('1234567890ABCDEF'); //16位

//加密向量
const iv = CryptoJS.enc.Utf8.parse('0123456789ABCDEF');

export default {
  //加密
  encrypt(word: any) {
    let encrypted = '';
    if (typeof word === 'string') {
      const srcs = CryptoJS.enc.Utf8.parse(word);
      encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
      });
    } else if (typeof word == 'object') {
      //对象格式的转成json字符串
      const data = JSON.stringify(word);
      const srcs = CryptoJS.enc.Utf8.parse(data);
      encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
      });
    }
    return encrypted.toString();
  },
  //解密
  decrypt(word: any) {
    const encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    // eslint-disable-next-line no-var
    var decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString(CryptoJS.enc.Utf8);
  },
  //md5
  md5(word: any) {
    return CryptoJS.MD5(word);
  },
};
